<template>
  <ContentWrap v-loading="infoLoading">
    <el-descriptions :title="t('judgeScene.card.title')" :column="5" direction="vertical" border>
      <el-descriptions-item :label="t('judgeScene.card.awardsName')">
        {{ awardsInfo.awardsName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.awards')">
        {{ awardsInfo.templateName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.stage')">
        {{ getJudgeStatus }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.currentRound')">
        {{ getCurrentRoundName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.studentNum')">
        <p>
          {{ t('instance.fields.total') }}:
          {{ (awardsInfo.masterCount ?? 0) + (awardsInfo.doctorCount ?? 0) }}
        </p>
        <p>{{ t('instance.fields.masterTotal') }}: {{ awardsInfo.masterCount }}</p>
        <p>{{ t('instance.fields.doctorTotal') }}: {{ awardsInfo.doctorCount }}</p>
      </el-descriptions-item>
      <template #extra>
        <!-- 最终名单 -->
        <el-button type="primary" plain @click="handleFinalList">
          {{ t('judgeScene.commonHeader.finalList') }}
        </el-button>
        <!-- 专业分组设置 -->
        <el-button
          v-if="currentRule?.isMajorGroup"
          type="primary"
          plain
          @click="handleGroupSettings"
        >
          {{ t('judgeScene.commonHeader.groupSettings') }}
        </el-button>
        <!-- 评委设置(非分组) -->
        <el-button v-if="!currentRule?.isMajorGroup" type="primary" plain @click="handleStageMajor">
          {{ t('judgeScene.commonHeader.judgeSettings') }}
        </el-button>
        <!-- 答辩设置 -->
        <el-button
          type="primary"
          plain
          @click="handleDebateSettings"
          :disabled="!currentRule?.isDebate"
        >
          {{ t('stageDebateTime.form.title') }}
        </el-button>
        <el-button
          type="primary"
          plain
          @click="handleDebateStudent"
          :disabled="!currentRule?.isDebate"
        >
          {{ t('judgeScene.final.debateStudent') }}
        </el-button>
      </template>
    </el-descriptions>
  </ContentWrap>

  <!-- 最终名单 -->
  <FinalWin ref="finalWinRef" />
  <!-- 专业分组设置 -->
  <StageMajorGroupSettings ref="stageMajorGroupSettingsRef" />
  <!-- 评委设置(非分组) -->
  <StageJudgeSelect ref="stageJudgeSelectRef" />
  <!-- 答辩设置 -->
  <StageDebateTimeForm ref="stageDebateTimeFormRef" @success="getInfo" />
  <!-- 答辩学生安排 -->
  <StageDebateStudentList ref="stageDebateStudentListRef" />
</template>

<script setup lang="ts">
import { InstanceApi, InstanceVO } from '@/api/awards/instance'
import { ReviewSceneApi, StageRoundVO } from '@/api/awards/reviewscene'
import { InstanceRuleApi, InstanceRuleVO } from '@/api/awards/instancerule'
import FinalWin from '@/views/awards/finalwin/index.vue'
import StageMajorGroupSettings from '@/views/awards/stagemajorgroup/StageMajorGroupSettings.vue'
import StageJudgeSelect from '@/views/awards/stagejudge/components/StageJudgeSelect.vue'
import StageDebateTimeForm from '@/views/awards/stagedebatetime/StageDebateTimeForm.vue'
import StageDebateStudentList from '@/views/awards/stageroundcandidate/StageDebateStudentList.vue'

defineOptions({ name: 'JudgeCommonHeader' })

const props = defineProps<{
  activeStage: string
}>()

const route = useRoute()
const router = useRouter() // 路由对象
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

// 评选状态
const getJudgeStatus = computed(() => {
  // 1=未开始 2=初评进行中 3=初评结束 4=终评进行中 5=终评结束
  switch (awardsInfo.value.judgeStatus) {
    case 1:
      return t('judgeScene.card.notStart')
    case 2:
      return t('judgeScene.card.initialUnderway')
    case 3:
      return t('judgeScene.card.initialEnd')
    case 4:
      return t('judgeScene.card.finalUnderway')
    case 5:
      return t('judgeScene.card.finalEnd')
    default:
      return t('judgeScene.card.notStart')
  }
})

// 初评信息
const initialStage = computed(() => {
  if (stageRoundInfos.value?.length === 2) {
    return stageRoundInfos.value?.[0]
  } else {
    // 没有初评
    return null
  }
})

// 终评信息
const finalStage = computed(() => {
  if (stageRoundInfos.value?.length === 2) {
    return stageRoundInfos.value?.[1]
  } else if (stageRoundInfos.value?.length === 1) {
    // 没有初评
    return stageRoundInfos.value?.[0]
  } else {
    return null
  }
})

// 计算当前阶段
const currentStage = computed(() => {
  if (props.activeStage === 'initial') {
    return initialStage.value
  } else {
    return finalStage.value
  }
})

// 计算当前阶段的名字
const getCurrentRoundName = computed(() => {
  return (currentStage.value?.round ?? []).find((item) => item.currentRound)?.name ?? 'N/A'
})

// 计算当前查看阶段的评奖阶段规则设置
const currentRule = computed(() => {
  if (props.activeStage === 'initial') {
    return (ruleInfo.value ?? []).find((item) => item.judgeStage === '1')
  } else {
    return (ruleInfo.value ?? []).find((item) => item.judgeStage === '2')
  }
})

const infoLoading = ref(false)
const awardsInfo = ref<InstanceVO>({} as InstanceVO)
// 为什么返回的是List? 因为初评+终评 最多可以有两条记录 如果没有初评则是一条记录
const ruleInfo = ref<InstanceRuleVO[]>()
const stageRoundInfos = ref<StageRoundVO[]>()
const getInfo = async () => {
  infoLoading.value = true
  const [awardsInfoRes, ruleInfoRes, stageRoundInfosRes] = await Promise.all([
    // 读取评奖基本信息
    InstanceApi.getInstance(awardsId.value),
    // 读取阶段规则设置信息(通用新)
    InstanceRuleApi.getInstanceRuleByAwardsId(awardsId.value),
    // 读取阶段和轮次信息信息
    ReviewSceneApi.getStageRound(awardsId.value)
  ])
  awardsInfo.value = awardsInfoRes
  ruleInfo.value = ruleInfoRes
  stageRoundInfos.value = stageRoundInfosRes
  infoLoading.value = false
}

// 最终名单
const finalWinRef = ref<InstanceType<typeof FinalWin>>()
const handleFinalList = () => {
  finalWinRef.value?.open(awardsId.value)
}

// 专业分组设置
const stageMajorGroupSettingsRef = ref<InstanceType<typeof StageMajorGroupSettings>>()
const handleGroupSettings = () => {
  if (currentRule.value) {
    stageMajorGroupSettingsRef.value?.open(currentRule.value)
  } else {
    message.error(`请先设置${props.activeStage === 'initial' ? '初评' : '终评'}评审规则`)
  }
}

// 评委设置(非分组)
const stageJudgeSelectRef = ref<InstanceType<typeof StageJudgeSelect>>()
const handleStageMajor = () => {
  if (currentRule.value) {
    stageJudgeSelectRef.value?.open(awardsId.value, undefined, currentRule.value.id)
  } else {
    message.error(`请先设置${props.activeStage === 'initial' ? '初评' : '终评'}评审规则`)
  }
}

// 答辩设置
const stageDebateTimeFormRef = ref<InstanceType<typeof StageDebateTimeForm>>()
const handleDebateSettings = () => {
  if (currentRule.value) {
    // 组件里面需要使用rule的信息 所以没有的话也是根本不让点
    stageDebateTimeFormRef.value?.open(awardsId.value, currentRule.value)
  } else {
    message.error(`请先设置${props.activeStage === 'initial' ? '初评' : '终评'}评审规则`)
  }
}

// 答辩学生安排
const stageDebateStudentListRef = ref<InstanceType<typeof StageDebateStudentList>>()
const handleDebateStudent = () => {
  if (currentRule.value) {
    stageDebateStudentListRef.value?.open(currentRule.value.id, awardsId.value)
  } else {
    message.error(`请先设置${props.activeStage === 'initial' ? '初评' : '终评'}评审规则`)
  }
}

const awardsId = ref('')
/** 初始化 **/
onMounted(() => {
  if (!route.params.instanceId) {
    router.push('/awards/instance')
    return
  }
  awardsId.value = route.params.instanceId as string
  // 查询评审进行信息
  getInfo()
})
</script>
